import ProxyHandler from "../sql/ProxyHandler";
import {BrowserWindow} from "electron";
import {v4 as uuidv4} from 'uuid';
import md5 from "crypto-js/md5";
import SystemLoyaltyMapper from "../dao/SystemLoyaltyMapper";
import SystemLoyalty from "../entity/SystemLoyalty";

export default class SystemLoyaltyService {
    private mapper: SystemLoyaltyMapper;
    private handler: ProxyHandler<SystemLoyalty>;

    constructor() {
        this.mapper = new SystemLoyaltyMapper()
        this.handler = new ProxyHandler<SystemLoyalty>();
    }

    async get(data){
        const win = BrowserWindow.getFocusedWindow();
        if(win == null){
            throw new Error("应用程序错误")
        }

        const card = await this.mapper.selectByMemberId(data)

        win.webContents.send('main-to-renderer', {
            name: 'loyalty-detail-success',
            event: 'event',
            data: card ? card : null
        })
    }

    async add(data){
        const win = BrowserWindow.getFocusedWindow();
        if(win == null){
            throw new Error("应用程序错误")
        }

        const card = await this.mapper.selectByMemberId(data.memberid)

        if(!card){
            const entity = new SystemLoyalty();
            entity.id = md5(uuidv4()).toString();
            entity.memberid = data.memberid;
            entity.cardno = new Date().getTime() + "";// npm install snowflake-uid
            entity.opendate = new Date().getTime()
            entity.balance = data.balance;
            const proxy = new Proxy(this.mapper.insert, this.handler)
            proxy(entity);
        }else{
            const entity = new SystemLoyalty();
            entity.id = card.id;
            entity.balance = card.balance + data.balance;
            const proxy = new Proxy(this.mapper.updateById, this.handler)
            proxy(entity);
        }

        win.webContents.send('main-to-renderer', {
            name: 'loyalty-operator-success',
            event: 'event',
            data: '操作成功'
        })
    }

    async sub(data){
        const win = BrowserWindow.getFocusedWindow();
        if(win == null){
            throw new Error("应用程序错误")
        }
        const card = await this.mapper.selectByMemberId(data.memberid)

        if(!card){
            const entity = new SystemLoyalty();
            entity.id = md5(uuidv4()).toString();
            entity.memberid = data.memberid;
            entity.cardno = new Date().getTime() + "";// npm install snowflake-uid
            entity.opendate = new Date().getTime()
            entity.balance = 0;
            const proxy = new Proxy(this.mapper.insert, this.handler)
            proxy(entity);
        }else{
            const entity = new SystemLoyalty();
            entity.id = card.id;
            entity.balance = (card.balance - data.balance) < 0 ? 0 : (card.balance - data.balance);
            const proxy = new Proxy(this.mapper.updateById, this.handler)
            proxy(entity);
        }

        win.webContents.send('main-to-renderer', {
            name: 'loyalty-operator-success',
            event: 'event',
            data: '操作成功'
        })
    }

}
